package com.qf.lja.service;

import com.qf.lja.dao.AddrDao;
import com.qf.lja.dao.CartDao;
import com.qf.lja.enity.Address;
import com.qf.lja.enity.Cart;
import com.qf.lja.enity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


@WebServlet(urlPatterns = "/order")
public class OrderService extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");


        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            resp.sendRedirect("login.jsp");
        }

        String u_id = req.getParameter("u_id");

        CartDao cartDao = new CartDao();
        AddrDao addrDao = new AddrDao();

        String productId = req.getParameter("productId");

        List<Cart> carts = cartDao.findAllByUserId(Integer.parseInt(u_id));
        List<Address> addrs = addrDao.selectAddr(Integer.parseInt(u_id));

        req.setAttribute("cartList", carts);
        req.setAttribute("addrList", addrs);

        req.getRequestDispatcher("/order.jsp").forward(req, resp);
    }
}
